package com.lisp.springframework.web.controller;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

@Controller
public class FileUploadController {
    
    private Logger logger = LogManager.getLogger();
	
	@RequestMapping("/fileUpload")
	@ResponseBody
	public String fileUpload(MultipartRequest req) {
	    try {
            Map<String, MultipartFile> fileMap = req.getFileMap();
            for(Entry<String, MultipartFile> e : fileMap.entrySet()) {
                String value = IOUtils.toString(e.getValue().getInputStream(), Charset.forName("UTF-8"));
                logger.info("key={}, value={}", e.getKey(), value);
            }
            return "OK";
        } catch (IOException e) {
            logger.error("文件上传出错", e);
            return "ERROR";
        }
	}
}
